package org.idea.qiyu.framework.retry.guava;


import com.github.rholder.retry.*;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Author linhao
 * @Date created in 9:18 上午 2021/10/4
 */
public class Application {

    static class ResponseInfo {
        int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public ResponseInfo() {
        }

        public static ResponseInfo buildSuccess(){
            ResponseInfo responseInfo = new ResponseInfo();
            responseInfo.setCode(1);
            return responseInfo;
        }
    }

    public static void main(String[] args) throws ExecutionException, RetryException {
        Callable<ResponseInfo> callable = new Callable<ResponseInfo>() {
            @Override
            public ResponseInfo call() throws Exception {
                System.out.println("call ...");
                return ResponseInfo.buildSuccess();
            }
        };

        Retryer<ResponseInfo> retryer = RetryerBuilder.<ResponseInfo>newBuilder()
                .retryIfResult(expectResult-> expectResult.getCode()==1)
                .retryIfException()
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))
                .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
                .withRetryListener(new MyRetryListener())
                .build();
        retryer.call(callable);
    }
}
